/*
 *    Copyright (c) 2018-2025, boss All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the boss developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: boss
 */
package com.hopes.bizz.emergency.resources.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hopes.bizz.emergency.resources.dto.ErEmergencyEvaluateRecordDTO;
import com.hopes.bizz.emergency.resources.entity.ErEmergencyEvaluateRecordEntity;
import com.hopes.bizz.emergency.resources.entity.ErEmergencyEvaluateRecordSummaryEntity;
import com.hopes.bizz.emergency.resources.mapper.ErEmergencyEvaluateRecordMapper;
import com.hopes.bizz.emergency.resources.service.ErEmergencyEvaluateRecordService;
import com.hopes.bizz.emergency.resources.service.ErEmergencyEvaluateRecordSummaryService;
import com.hopes.boss.common.core.util.R;
import com.hopes.boss.common.security.util.SecurityUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 评估记录表
 *
 * @author cx
 * @date 2024-01-11 20:00:06
 */
@Service
public class ErEmergencyEvaluateRecordServiceImpl extends ServiceImpl<ErEmergencyEvaluateRecordMapper, ErEmergencyEvaluateRecordEntity> implements ErEmergencyEvaluateRecordService {
    @Resource
    private  ErEmergencyEvaluateRecordSummaryService erEmergencyEvaluateRecordSummaryService;
    @Override
    public R addEvaluateRecord(ErEmergencyEvaluateRecordDTO erEmergencyEvaluateRecord) {
        List<ErEmergencyEvaluateRecordEntity> recordList = erEmergencyEvaluateRecord.getRecordList();
        this.saveBatch(recordList);
        //综合得分
        double compositeScore = 0;
        if (recordList != null || !recordList.isEmpty()) {
            double totalScore = recordList.stream()
                    .mapToDouble(ErEmergencyEvaluateRecordEntity::getSingleScore)
                    .sum();
            compositeScore = totalScore / recordList.size();
            ErEmergencyEvaluateRecordEntity recordEntity = recordList.get(0);
            ErEmergencyEvaluateRecordSummaryEntity summaryEntity = new ErEmergencyEvaluateRecordSummaryEntity();
            summaryEntity.setCompositeScore(compositeScore);
            summaryEntity.setObjCode(recordEntity.getObjCode());
            summaryEntity.setObjId(recordEntity.getObjId());
            summaryEntity.setOrgId(recordEntity.getOrgId());
            summaryEntity.setEvaluateTime(new Date());
            return R.ok(erEmergencyEvaluateRecordSummaryService.save(summaryEntity));
        }
        return R.ok(null,"未进行有效评估");
    }
}